package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;

import model.Duty;
import model.Education;
import dao.EducationDAO;

public class EducationDAOImpl implements EducationDAO {
	private DBUtil dbu = new DBUtil();
	private Connection conn = null;
	private PreparedStatement pstat = null;
	private ResultSet rs = null;

	public List<Education> getAll() {
		List<Education> list = new ArrayList<Education>();
		try {
			conn = dbu.getConnection();
			String sql = "select * from education";
			pstat = conn.prepareStatement(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				Education edu = new Education();
				edu.setId(rs.getInt("id"));
				edu.setName(rs.getString("name"));
				list.add(edu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return list;
	}

	public Education getById(int id) {
		conn = dbu.getConnection();
		Education edu = new Education();
		try {
			pstat = conn.prepareStatement("select * from Education where id="
					+ id);
			rs = pstat.executeQuery();
			while (rs.next()) {
				edu.setId(rs.getInt("id"));
				edu.setName(rs.getString("name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return edu;
	}

	public Education getByName(String name) {
		conn = dbu.getConnection();
		Education edu = new Education();
		try {
			pstat = conn
					.prepareStatement("select id,name from EDUCATION where name='"
							+ name+"'");
			rs = pstat.executeQuery();
			if (rs.next()) {
				edu.setId(rs.getInt("id"));
				edu.setName(rs.getString("name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return edu;
	}

}
